package com.example.demo.controller;

import com.example.demo.dao.DTO.MovieDTO;
import com.example.demo.dao.entity.Comment;
import com.example.demo.dao.entity.Movie;
import com.example.demo.dao.entity.SysUser;
import com.example.demo.service.CommentService;
import com.example.demo.service.MovieService;
import com.example.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询电影列表
     * @param model
     * @param principal
     * @return
     */
    @GetMapping("/list")
    public String getCommentList(Model model,Principal principal) {
        String username = principal.getName();
        List<Movie> movieList = movieService.lambdaQuery().eq(Movie::getStatus, 0).list();
        List<Long> ids = movieList.stream().map(a -> a.getId()).collect(Collectors.toUnmodifiableList());

        List<MovieDTO> list = commentService.selectAll();

        List<Comment> commentList = commentService.lambdaQuery().eq(Comment::getStatus, 0)
                .in(Comment::getMovieId, ids).list();

        for(Movie m: movieList){
            for(Comment c : commentList){
                if(c.getMovieId().equals(m.getId())){
                    m.setComments((m.getComments()==null?"":m.getComments())+"<br/>评论人："+c.getCreatePerson()+"-评论内容"+c.getComment());
                }
            }
        }
        model.addAttribute("list", movieList);
        return "comment/list";
    }

    /**
     * 新增评论页面
     * @param model
     * @param comment
     * @param id
     * @return
     */
    @GetMapping("addpage")
    public String addpage(Model model, @ModelAttribute Comment comment, @RequestParam(value = "id",required = false) Integer id,
                          @RequestParam(value = "mid",required = false)  Integer mid){

        if(!StringUtils.isEmpty(id)){
            Comment byId = commentService.getById(id);
            model.addAttribute("comment",byId);
        }
        List<Movie> list = list = movieService.lambdaQuery().eq(Movie::getStatus, 0).list();
        if(mid != null){
            Movie movie = movieService.getById(mid);
            model.addAttribute("movie", movie);
        }
        model.addAttribute("movies", list);

        return "comment/add";
    }

    /**
     * 新增或修改评论
     * @param model
     * @param comment
     * @param principal
     * @return
     */
    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    public String saveOrUpdate(Model model, Comment comment, Principal principal) {
        String loginName = principal.getName();
        SysUser one = sysUserService.lambdaQuery().eq(SysUser::getLoginName, loginName).one();
        Date now = new Date();
        comment.setCreatePerson(loginName);
        comment.setUpdatePerson(loginName);
        comment.setUserId(one.getId());
        if(comment.getId() == null){
            comment.setCreateTime(now);
        }
        comment.setUpdateTime(now);
        commentService.saveOrUpdate(comment);
        return getCommentList(model,principal);
    }


    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public String deleteCommentById( @PathVariable Long id) {
        //逻辑删除
        commentService.lambdaUpdate().eq(Comment::getId,id).set(Comment::getStatus,1).update();
        return "success";
    }
}
